<?php
class Login extends CI_Controller{
	function index()
	{
		$data['error_message'] = "";
		$this->load->view('user/login', $data);	
	}
	
	function validate_credentials()
	{		
		$this->load->model('user/User_Model', 'user_model');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$query = $this->user_model->validate_member($username, $password);
		
		if($query) // if the user's credentials validated...
		{
			$user = $this->user_model->get_by(array('username' => $username));
			$data = array(
				'username' => $username,
				'is_logged_in' => true,
				'role_id' => $user->role_id,
			);
			$this->session->set_userdata($data);
			redirect(base_url());
		}
		else // incorrect username or password
		{
			$data['error_message'] = "Username or password is not correct";
			$this->load->view('user/login', $data);
		}
	}	
	
	function logout()
	{
		$this->session->sess_destroy();
		redirect(base_url());
	}
}
?>